package com.cn.config.security.service;

import com.cn.entity.User;
import com.cn.service.UserService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class CustomerUserDetailsService implements UserDetailsService {
    @Resource
    private UserService userService;

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据用户名查询用户信息
        User user = userService.findUserByUserName(username);
        //根据用户信息查询权限信息
        if (user == null) {
            throw new UsernameNotFoundException("用户名或密码出错");
        }
        //封装成UserDetails对象返回
        return user;

    }
}
